package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.FacultySummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.service.FacultyServiceAsync;
import com.rattsoftware.client.service.UserServiceAsync;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.FacultyView;
import com.rattsoftware.server.admin.model.Faculty;

public class FacultyController extends Controller {

	AppMessages appMessages = GWT.create(AppMessages.class); 

    private FacultyView facultyListView;
    private UserServiceAsync userService;
    private FacultyServiceAsync facultyService;
    
    public FacultyController() {
        super();
        registerEventTypes(AppEvents.Init);
        registerEventTypes(AppEvents.FacultyView);
    }
	
    @Override
    protected void initialize() {
        GWT.log("FacultyController : initialize");
        userService = Registry.get(webclient.USER_SERVICE);
        facultyService = Registry.get(webclient.FACULTY_SERVICE);
        facultyListView = new FacultyView(this);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_FACULTY, facultyListView);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.CREATE_FACULTY, facultyListView);
        RefreshablePublisher.get().subscribe(RefreshableEvent.Type.REFRESH_FACULTY_LIST, facultyListView);
    }

    @Override
    public void handleEvent(AppEvent event) {
    	GWT.log("FacultyController : handleEvent");
        EventType type = event.getType();
        if (type == AppEvents.FacultyView) {
            forwardToView(facultyListView, event);
        } else if (type == AppEvents.Init) {
            forwardToView(facultyListView, event);
        }
    }
    
    public void getFacultyList(final PagingLoadConfig pagingLoadConfig, 
            final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<FacultySummary>> callback) {
    	GWT.log("FacultyController : getting Faculties");
    	/*userService.getUsers(PagingUtil.createPagingLoadConfig(pagingLoadConfig), 
    			new RattsAsyncCallback<PagingLoadResult<User>>() {
            @Override
			public void onSuccess(PagingLoadResult<User> result) {*/
    	
                List<FacultySummary> results = new ArrayList<FacultySummary>();
                //List<User> data = result.getData();
                List<Faculty> data = TestData.getFaculties();
                for (Faculty d : data) {
                    results.add(new FacultySummary(d));
                }
                ProgressIndicator.hideProgressBar();
                //callback.onSuccess(new BasePagingLoadResult<UserSummary>(results, result.getOffset(), result.getTotalLength()));
                callback.onSuccess(new BasePagingLoadResult<FacultySummary>(results, 0, 20));
            /*}
    	});*/
    }
    
    public void forwardToNewWizard() {
    	GWT.log("FacultyController : forwardToNewWizard");
        Dispatcher dispatcher = Dispatcher.get();
        AppEvent event = new AppEvent(AppEvents.NewFaculty);
        GWT.log("dispatching event=" +event);
    	dispatcher.dispatch(event);
    }
    
    public void forwardToEditFacultyController(FacultySummary facultySummary) {
		GWT.log("FacultyController : forwardToEditFacultyController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.EditFaculty, facultySummary);
	}
    
    public void forwardToNewEditFacultyController() {
		GWT.log("FacultyController : forwardToNewEditFacultyController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewFaculty);
	}
    
    public void forwardToNewDepartmentController(FacultySummary facultySummary) {
		GWT.log("FacultyController : forwardToNewDepartmentController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewDepartment, facultySummary);
	}
    
    public void forwardToViewDepartmentsController(FacultySummary facultySummary) {
		GWT.log("FacultyController : forwardToViewDepartmentsController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.ViewDepartments, facultySummary);
	}
}